package com.lry.basic.insterest;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import net.coobird.thumbnailator.Thumbnails;
import sun.java2d.SunGraphics2D;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/7/30
 */
public class Test {
    static final String IMAGE_SUFFIX = ".png";
    static final int HEAD_URL_WIDTH = 100;
    static final int HEAD_URL_HEIGHT = 100;

//    public static String drawImage(String posterImgUrl, String tempQrCodeImgUrl, String headImgUrl) throws IOException {
//
//        BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
//        Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();
//
//        BufferedImage qrCodeImage = ImageIO.read(new URL(tempQrCodeImgUrl));
//        BufferedImage headImage = ImageIO.read(new URL(headImgUrl));
//
//        //设置圆形头像
//        BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(), BufferedImage.TYPE_INT_RGB);
//
//        Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
//        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
//        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(), headImage.getHeight(),
//                Transparency.TRANSLUCENT);
//        roundHeadGraphics = roundHeadImg.createGraphics();
//        // 使用 setRenderingHint 设置抗锯齿
//        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        roundHeadGraphics.setClip(shape);
//        roundHeadGraphics.drawImage(headImage, 0, 0, null);
//        roundHeadGraphics.dispose();
//
//        posterBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth() - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);
//        posterBufImageGraphics.drawImage(roundHeadImg, 50, 100, HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null);
//        posterBufImageGraphics.dispose();
//
//        ByteArrayOutputStream bs = new ByteArrayOutputStream();
//        ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
//        ImageIO.write(posterBufImage, "png", imgOut);
//        InputStream inSteam = new ByteArrayInputStream(bs.toByteArray());
//        String url = OSSFactory.build().uploadSuffix(inSteam, IMAGE_SUFFIX);
//        return url;//返回合成的图片地址url
//    }
    public static void poster() throws Exception {
        String qrCodeUrl = "https://images.xiaogemaicai.cn/StoreQRCodes/201001.jpg";
        String goodsUrl = "https://images.xiaogemaicai.cn/20180517/1745275087aac1.jpg";
        String avatarUrl = "https://wx.qlogo.cn/mmopen/vi_32/bvlq2OnCCSQxpfyJkgvSn3AvHaqB5UFFTasHWZD4ibeHb7b5wW6bVkqbdtTiaEw45GfLYibudiaORHBrRsy9tKgabA/132";
        BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
        BufferedImage goodsImage = ImageIO.read(new URL(goodsUrl));
        BufferedImage avatarImage = ImageIO.read(new URL(avatarUrl));
        BufferedImage all = new BufferedImage(300,500,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) all.getGraphics();
        g.setBackground(Color.WHITE);//设置背景色
        g.clearRect(0, 0, 300, 500);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        BufferedImage roundHeadImg = circle(avatarUrl);

        g.drawImage(qrCodeImage, 0, 300, 100, 100, null);
        g.drawImage(goodsImage, 50, 100, 100,100, null);
        g.drawImage(roundHeadImg.getScaledInstance(100, 100, Image.SCALE_DEFAULT), 0, 0, null);
        g.dispose();
        ImageIO.write(all, "png", new File("d:\\demo1.png"));
    }

    private static BufferedImage circle(String avatar_img) throws Exception {
        BufferedImage avatar = ImageIO.read(new URL(avatar_img));
        BufferedImage newAvatar = new BufferedImage(avatar.getWidth(), avatar.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, avatar.getWidth(), avatar.getHeight());
        Graphics2D g2 = newAvatar.createGraphics();
        newAvatar = g2.getDeviceConfiguration().createCompatibleImage(avatar.getWidth(), avatar.getHeight(), Transparency.TRANSLUCENT);
        g2 = newAvatar.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(newAvatar.getWidth(), newAvatar.getHeight()));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2 = newAvatar.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRoundRect(0, 0,avatar.getWidth(), avatar.getHeight(), 150, 150);
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(avatar, 0, 0, avatar.getWidth(), avatar.getHeight(), null);
        g2.dispose();
        return newAvatar;
    }

    public static void main(String[] args) throws Exception {
//        Test.poster();
//        Thumbnails.of("d:\\1.jpg").size(100, 100).toFile(
//                "d:\\2.jpg");
//        Thumbnails.of("d:\\1.jpg") .size(70,70)
//                .outputQuality(1f).toFile(
//                "d:\\2.jpg");

    }


}
